package cn.hlyc.mq.asyn;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 生产者
 */



public class AsynProducer {
    public static void main(String[] args)throws Exception {
        //创建生产者实例
        DefaultMQProducer producer = new DefaultMQProducer("mq-asyn-producer-group");
        //设置mq的nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //启动生产者
        producer.start();

        byte[] msgByteArray = "异步".getBytes(RemotingHelper.DEFAULT_CHARSET);

        Message message = new Message("asyn-topic", "asyn-tag", msgByteArray);

        producer.send(message,new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {

            }
        });

        //发消息
        SendResult sendResult = producer.send(message);
        System.out.println(sendResult);

        //关闭资源
        producer.shutdown();


    }
}
